#include <string.h>
#include <iostream>
using std::cout;
using std::endl;
using std::ostream;


class String
{
public:
    String()
    /* : _pstr(nullptr) */
    : _pstr(new char[1]())
    {
        cout << "String()" << endl;
    }

    String(const char *pstr)
    : _pstr(new char[strlen(pstr) + 1]())
    {
        cout << "String(const char *)" << endl;
        strcpy(_pstr, pstr);
    }

    //移动构造
    String(String && rhs)
    : _pstr(rhs._pstr)//浅拷贝
    {
        cout << "String移动构造" << endl;
        rhs._pstr = nullptr;
    }

    //拷贝构造
    String(const String & rhs)
    : _pstr(new char[strlen(rhs._pstr) + 1]())
    {
        cout << "String(const String &)" << endl;
        strcpy(_pstr, rhs._pstr);
    }

    //移动赋值函数
    //s1 = std::move(s1)
    //String && rhs = std::move(s1)
    //
    String & operator=(String && rhs){
        cout << "移动赋值函数" << endl;
        if(this != &rhs){ //自复制判断
            delete [] _pstr;
            _pstr = rhs._pstr;//浅拷贝
            rhs._pstr = nullptr;
        }
        return *this;
    }


    //赋值运算符函数
    String & operator=(const String & rhs)
    {
        cout << "String &operator=(const String &)" << endl;
        if(this != &rhs)
        {
            if(_pstr)
            {
                delete [] _pstr;
            }
            _pstr = new char[strlen(rhs._pstr) + 1]();
            strcpy(_pstr, rhs._pstr);
        }
        return *this;
    }

    size_t length() const
    {
        size_t len = 0;
        if(_pstr)
        {
            len = strlen(_pstr);
        }

        return len;

    }

    const char * c_str() const
    {
        return _pstr;
           
    }


    ~String()
    {
        cout << "~String()" << endl;
        if(_pstr)
        {
            delete [] _pstr;
            _pstr = nullptr;
        }
    }

    void print() const
    {
        if(_pstr)
        {
            cout << "_pstr = " << _pstr << endl;
        }else{
            cout << endl;
        }
    }

    friend
    ostream & operator<<(ostream & os,const String & rhs);
private:
    char * _pstr;
};

ostream & operator<<(ostream & os,const String & rhs){
    os << rhs._pstr;
    return os;
}


void test0(){
    String s1("hello");
    String s2 = s1;
    String s3 = "hello";

    cout << endl;
    String s4("hello");
    s4 = String("wangdao");
    s4 = s3;
}

void test1(){
    int a = 1;
    cout << &a << endl;
    /* &(std::move(a)); */
    int && ref = std::move(a);
    cout << &ref << endl;
    ref = 100;
    cout << a << endl;
    cout << &a << endl;
    cout << endl;


    String s1("hello");
    printf("%p\n",s1.c_str());
	cout << "s1:" << s1 << endl;
    //会影响s1对象本体的内容
    //最终的效果是s1申请的堆空间的管理权被移交给了s2对象
	String s2 = std::move(s1);//移动构造
	/* cout << "s1:" << s1 << endl; */
    cout << "s2:" << s2 << endl;
    printf("%p\n",s2.c_str());
}

void test2(){
    String s1("hello");
    s1 = std::move(s1);//移动赋值函数
    //如果移动赋值函数中没有自复制判断
    //那么会使s1的_pstr变成空指针
    cout << s1 << endl;
    cout << "over" << endl;
}

int main(void){
    test2();
    return 0;
}
